package com.linktel.productionLine.EasyExcel.util;

import com.alibaba.excel.annotation.ExcelProperty;
import com.linktel.productionLine.EasyExcel.ExcelComment;
import com.linktel.productionLine.EasyExcel.ExcelNotation;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用easyExcel的工具类
 *
 * @Author HuangJiaHui
 * @Date 2023/9/11 19:56
 */
public class Methods {
    /**
     * 获取批注Map
     *
     * @param clazz 类class
     * @return java.util.Map<java.lang.Integer, java.lang.String>
     * @author SunLingDa
     * @date 2022/11/3 13:24
     */
    public static Map<Integer, ExcelComment> getNotationMap(Class<?> clazz) {
        Map<Integer, ExcelComment> notationMap = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(ExcelNotation.class) ) {
                continue;
            }
            ExcelComment excelComment = new ExcelComment();
            ExcelNotation excelNotation = field.getAnnotation(ExcelNotation.class);
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            excelComment.setRemarkValue(excelNotation.value());
            excelComment.setRemarkColumnWide(excelNotation.remarkColumnWide());
            excelComment.setRemarkRowHigh(excelNotation.remarkRowHigh());
            notationMap.put(excelProperty.index(), excelComment);
        }
        return notationMap;
    }

    public static String getDateFromExcel(String attendanceDateStr) {
        attendanceDateStr = attendanceDateStr.replace('/', '-');
        StringBuilder attendanceDate = new StringBuilder(attendanceDateStr);
        if (attendanceDate.lastIndexOf("-") == 6) {
            attendanceDate.insert(5, "0");
        }
        if (attendanceDate.length() == 9) {
            attendanceDate.insert(8, "0");
        }

        attendanceDateStr = new String(attendanceDate);
        return attendanceDateStr;
    }
}
